﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mesoBoard.Data.Repositories
{
    public class SmileyRepository : ISmileyRepository
    {
        private mbEntities db;

        public SmileyRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public string GetSmileyURL(string SmileyCode)
        {
            Smiley TheSmiley = db.Smilies.FirstOrDefault(x => x.Code == SmileyCode);
            if (TheSmiley == null)
                return "";
            else
               return TheSmiley.ImageURL;
        }

        public Smiley GetSmiley(string SmileyCode)
        {
            return db.Smilies.FirstOrDefault(x => x.Code.ToLower() == SmileyCode.ToLower());
        }

        public Smiley GetSmiley(int SmileyID)
        {
            return db.Smilies.FirstOrDefault(x => x.SmileyID == SmileyID);
        }

        public IEnumerable<Smiley> GetSmilies()
        {
            return db.Smilies.AsEnumerable();
        }

        public void SaveSmiley(Smiley TheSmiley)
        {
            if (TheSmiley.EntityKey == null)
                TheSmiley.EntityKey = ((Smiley)db.GetObjectByKey(new System.Data.EntityKey("mbEntities.Smilies", "SmileyID", TheSmiley.SmileyID))).EntityKey;

            db.Smilies.ApplyCurrentValues(TheSmiley);
            db.SaveChanges();
        }

        public void DeleteSmiley(int SmileyID)
        {
            db.Smilies.DeleteObject(db.Smilies.FirstOrDefault(x => x.SmileyID == SmileyID));
            db.SaveChanges();
        }

        public void CreateSmiley(Smiley TheSmiley)
        {
            db.Smilies.AddObject(TheSmiley);
            db.SaveChanges();
        }
    }
}
